﻿using System;
using System.Windows.Forms;
using UIapp.Interfaces;


namespace UIapp
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            try
            {
                InitializeComponent();
                string publicacion = "";
                if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed)
                {
                    System.Deployment.Application.ApplicationDeployment ad = System.Deployment.Application.ApplicationDeployment.CurrentDeployment;
                    publicacion = ad.CurrentVersion.Major.ToString() + "." +
                       ad.CurrentVersion.Minor.ToString() + "." + ad.CurrentVersion.Build.ToString() + "." + ad.CurrentVersion.Revision.ToString();
                }


                this.Text = this.Text + "           Versión.: " + Application.ProductVersion + " publicación.: " + publicacion + " F. Compilación.: " + Comun.FechaCompilacion.ToString("dd.MM.yyyy");

                Comun.frmMainSize = this.Size;
                Comun.frmMainLocation = this.Location;
                string url = Comun.systemConfig.RUTA_IMAGENES + "Header_bg.jpg";
                this.pnlMainHead.BackgroundImage = new System.Drawing.Bitmap(
                    Comun.systemConfig.RUTA_IMAGENES + "Header_bg.jpg");

                this.pnlMainHead.BackgroundImageLayout = ImageLayout.Stretch;

                this.pnlBuscar.BackgroundImage = new System.Drawing.Bitmap(
                     Comun.systemConfig.RUTA_IMAGENES + "bg_buscar.png");

                this.pnlBuscar.BackgroundImageLayout = ImageLayout.Stretch;

                this.btnNuevo.Image = new System.Drawing.Bitmap(
                     Comun.systemConfig.RUTA_IMAGENES + "Nuevo.png");

                this.btnModificar.Image = new System.Drawing.Bitmap(
                  Comun.systemConfig.RUTA_IMAGENES + "Modificar.png");

                this.btnEliminar.Image = new System.Drawing.Bitmap(
                  Comun.systemConfig.RUTA_IMAGENES + "Eliminar.png");

                this.btnBuscar.Image = new System.Drawing.Bitmap(
                  Comun.systemConfig.RUTA_IMAGENES + "Buscar.png");

                this.btnRefrescar.Image = new System.Drawing.Bitmap(
                 Comun.systemConfig.RUTA_IMAGENES + "Refrescar.png");

                this.lblNombreCia.Text = Comun.utilidades.NOMBRE_CIA;

                this.imgUser.Image = new System.Drawing.Bitmap(
                    Comun.systemConfig.RUTA_IMAGENES + "user.png");

                this.radDesktopAlert1.ContentImage = new System.Drawing.Bitmap(
                              Comun.systemConfig.RUTA_IMAGENES + "bell.png");

                if (Comun.usuario != null)
                {
                    this.lblUser.Text = string.Format("{0}-{1}", Comun.usuario.ID.ToString("00"), Comun.usuario.ToString());
                }
            }
            catch (Exception ex)
            {
                Comun.ShowMessage(ex);
                this.Close();
            }
        }

        #region Variables
        private Interfaces.IPantalla _pantallaActual = null;
        int TimerCount = 0;
        #endregion

        #region Tratamiento de eventos
        private void frmMain_SizeChanged(object sender, EventArgs e)
        {
            Comun.frmMainSize = this.Size;
        }

        private void frmMain_LocationChanged(object sender, EventArgs e)
        {
            Comun.frmMainLocation = this.Location;
        }

        private void frmMain_MdiChildActivate(object sender, EventArgs e)
        {
            if (this.ActiveMdiChild != null)
            {
                this._pantallaActual = (IPantalla)this.ActiveMdiChild;
                this.MostrarComandosSoportados(this._pantallaActual.ComandosSoportados);
            }
            else
            {
                this.MostrarComandosSoportados(0);
                this._pantallaActual = null;
            }

        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            this.MostrarComandosSoportados(0);
        }

        private void btnRefrescar_Click(object sender, EventArgs e)
        {
            if (this._pantallaActual != null)
                this._pantallaActual.Refrescar();
        }

        private void btnBuscar_Click(object sender, EventArgs e)
        {
            if (this._pantallaActual != null)
                this._pantallaActual.Buscar();
        }

        private void btnEliminar_Click(object sender, EventArgs e)
        {
            if (this._pantallaActual != null)
                this._pantallaActual.Eliminar();
        }

        private void btnModificar_Click(object sender, EventArgs e)
        {
            if (this._pantallaActual != null)
                this._pantallaActual.Modificar();
        }

        private void btnNuevo_Click(object sender, EventArgs e)
        {
            if (this._pantallaActual != null)
                this._pantallaActual.Nuevo();
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            this.TimerCount++;
            if (this.TimerCount == 5)
            {
                errorProvider1.SetError(this.lblMsg, null);
                this.lblMsg.Text = "";
                this.timer.Enabled = false;
                this.TimerCount = 0;

            }
        }

        private void frmMain_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyData)
            {
                case Keys.F2:
                    this.btnNuevo.PerformClick();
                    break;
                case Keys.F3:
                    this.btnModificar.PerformClick();
                    break;
                case Keys.F12:
                    this.btnEliminar.PerformClick();
                    break;
                case Keys.F9:
                    this.btnBuscar.PerformClick();
                    break;
                case Keys.F5:
                    this.btnRefrescar.PerformClick();
                    break;
            }
        }

        private void ciudadesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Vistas.frmCatalogos frm = new Vistas.frmCatalogos(Comun.Catalogos.Ciudades);
            frm.MdiParent = this;
            frm.Show();
        }

        private void bancosToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Vistas.frmCatalogos frm = new Vistas.frmCatalogos(Comun.Catalogos.Bancos);
            frm.MdiParent = this;
            frm.Show();
        }

        private void monedasToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Vistas.frmCatalogos frm = new Vistas.frmCatalogos(Comun.Catalogos.Monedas);
            frm.MdiParent = this;
            frm.Show();
        }

        private void nroOriginalesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Vistas.frmOriginales frm = new Vistas.frmOriginales(false);
            frm.MdiParent = this;
            frm.Show();
        }

        private void nroAdicionalesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Vistas.frmOriginalesAdicionales frm = new Vistas.frmOriginalesAdicionales();
            frm.MdiParent = this;
            frm.Show();
        }

        private void productosToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Vistas.frmProductos frm = new Vistas.frmProductos(true);
            frm.MdiParent = this;
            frm.Show();
        }

        private void descripcionesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Vistas.frmCatalogos frm = new Vistas.frmCatalogos(Comun.Catalogos.Descripciones);
            frm.MdiParent = this;
            frm.Show();
        }

        private void categoriasToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Vistas.frmCatalogos frm = new Vistas.frmCatalogos(Comun.Catalogos.Categorias);
            frm.MdiParent = this;
            frm.Show();
        }

        private void medidasToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Vistas.frmCatalogos frm = new Vistas.frmCatalogos(Comun.Catalogos.Medidas);
            frm.MdiParent = this;
            frm.Show();
        }

        private void departamentosToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Vistas.frmCatalogos frm = new Vistas.frmCatalogos(Comun.Catalogos.Departamentos);
            frm.MdiParent = this;
            frm.Show();
        }

        private void modelosToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Vistas.frmCatalogos frm = new Vistas.frmCatalogos(Comun.Catalogos.Modelos);
            frm.MdiParent = this;
            frm.Show();
        }

        private void versionesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Vistas.frmCatalogos frm = new Vistas.frmCatalogos(Comun.Catalogos.Versiones);
            frm.MdiParent = this;
            frm.Show();
        }

        private void marcasToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Vistas.frmCatalogos frm = new Vistas.frmCatalogos(Comun.Catalogos.Marcas);
            frm.MdiParent = this;
            frm.Show();
        }

        private void suplidoresToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Vistas.frmSuplidores frm = new Vistas.frmSuplidores(false);
            frm.MdiParent = this;
            frm.Show();
        }

        private void clientesToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Vistas.frmClientes frm = new Vistas.frmClientes(false);
            frm.MdiParent = this;
            frm.Show();
        }

        private void zonasToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Vistas.frmZonas frm = new Vistas.frmZonas();
            frm.MdiParent = this;
            frm.Show();
        }

        private void rutasToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Vistas.frmRutas frm = new Vistas.frmRutas();
            frm.MdiParent = this;
            frm.Show();
        }

        private void ajusteInventarioToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Dialogos.frmAjustesInventario frm = new Dialogos.frmAjustesInventario();
            frm.MdiParent = this;
            frm.Show();
        }

        private void ComprasToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Vistas.frmCompras frm = new Vistas.frmCompras();
            frm.MdiParent = this;
            frm.Show();
        }

        private void facturasToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Vistas.frmFacturas frm = new Vistas.frmFacturas();
            frm.MdiParent = this;
            frm.Show();
        }

        private void ayudaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new Dialogos.frmFacturasPagos(null).Show();
        }

        private void acercaDeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new Dialogos.frmAcercaDe().ShowDialog();
        }

        private void ajustesInventarioToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Vistas.frmAjustesInventario frm = new Vistas.frmAjustesInventario();
            frm.MdiParent = this;
            frm.Show();
        }

        private void buscarMovimientosToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Vistas.frmProductosMovimientos frm = new Vistas.frmProductosMovimientos();
            frm.MdiParent = this;
            frm.Show();
        }

        private void ncfsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Vistas.frmNcf frm = new Vistas.frmNcf();
            frm.MdiParent = this;
            frm.Show();
        }

        private void cotizacionesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Vistas.frmCotizaciones frm = new Vistas.frmCotizaciones();
            frm.MdiParent = this;
            frm.Show();
        }

        private void notasDeCreditoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Vistas.frmNc frm = new Vistas.frmNc();
            frm.MdiParent = this;
            frm.Show();
        }

        private void recibosDePagosToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Vistas.frmRecibosPagos frm = new Vistas.frmRecibosPagos();
            frm.MdiParent = this;
            frm.Show();
        }
        #endregion

        #region Metodos auxiliares
        private void MostrarComandosSoportados(int comandosSoportados)
        {

            // Botones y opciones de menú
            if ((comandosSoportados & (int)ComandosPantalla.Buscar) > 0)
            {
                btnBuscar.Visible = true;
            }
            else
            {
                btnBuscar.Visible = false;
            }

            if ((comandosSoportados & (int)ComandosPantalla.Eliminar) > 0)
            {
                btnEliminar.Visible = true;
            }
            else
            {
                btnEliminar.Visible = false;

            }

            if ((comandosSoportados & (int)ComandosPantalla.Modificar) > 0)
            {
                btnModificar.Visible = true;
            }
            else
            {
                btnModificar.Visible = false;
            }

            if ((comandosSoportados & (int)ComandosPantalla.Nuevo) > 0)
            {
                btnNuevo.Visible = true;
            }
            else
            {
                btnNuevo.Visible = false;
            }

            if ((comandosSoportados & (int)ComandosPantalla.Refrescar) > 0)
            {
                btnRefrescar.Visible = true;
            }
            else
            {
                btnRefrescar.Visible = false;
            }
        }

        public void ShowMessage(string msg)
        {
            this.lblMsg.Text = msg;
            errorProvider1.SetError(this.lblMsg, msg);
            this.timer.Enabled = true;
        }
        #endregion
    }
}
